#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
// #include <

int main(int argc, char ** argv) {
	if (argc != 2) {
		printf("Usage: ./write filedir\n");
		return -1;
	}
	int ret, fd;
	fd = open(argv[1], O_RDWR | O_CREAT, 0755);
	if (-1 == fd) {
		perror("open error");
		return errno;
	}
	ftruncate(fd, 1024);

	char *mm = (char*) mmap(0, 1024, PROT_WRITE, MAP_SHARED, fd, 0);
	if (MAP_FAILED == mm) {
		perror("mmap");
		close(fd);
		return errno;
	}
	close(fd);

	int idx = 0;
	while (idx ++ < 10) {
		if (idx % 2 == 0)
			strcpy(mm, "HelloWorld");
		else
			strcpy(mm, "i am maqian");
		sleep(1);
	}

	ret = munmap(mm, 1024);
	if (-1 == ret){
		perror("munmap");
	}

	return 0;
}
